﻿using ArtMath.Core.Helper;
using ArtMath.Resource;
using System;
using System.Windows.Forms;

namespace ArtMath.Window
{
    public partial class RenameWindow : Form
    {
        public RenameWindow(string initName, Predicate<string> checkExist, Func<string, string> promptMsg)
        {
            InitializeComponent();
            textBox1.Text = initName;
            btnOK.Click += (object sender, EventArgs e) =>
            {
                if (NewName == initName)
                {
                    DialogResult = DialogResult.Cancel;
                }
                else
                {
                    if (checkExist(NewName))
                    {
                        if (MessageBox.Show(promptMsg(NewName), Resources.Translate("Ask"), MessageBoxButtons.YesNoCancel, MessageBoxIcon.Question) == DialogResult.Yes)
                        {
                            textBox1.Text = Utility.GetNextAvailableName(NewName, checkExist);
                            DialogResult = DialogResult.OK;
                        }
                    }
                    else
                    {
                        DialogResult = DialogResult.OK;
                    }
                }
            };
        }
        public string NewName
        {
            get { return textBox1.Text; }
        }
        private void btnCancel_Click(object sender, EventArgs e)
        {
            DialogResult = DialogResult.Cancel;
        }
    }
}
